<template>
  <div class="home">
    <!-- <img alt="Vue logo" src="../assets/logo.png">
    <HelloWorld msg="Welcome to Your Vue.js + TypeScript App"/> -->
    <div class="head">
      测试主题(多选)：
      <el-checkbox-group v-model="checkTitle" size="large">
        <el-checkbox-button v-for="(item, index) in formInfo1" :key="index" :label="item.title">
          {{ item.title }}
        </el-checkbox-button>
      </el-checkbox-group>
      <el-switch v-model="isRandom" size="large" active-text="随机" inactive-text="顺序" />
    </div>
    <div class="main" v-if="questionTable.length > 0">
      <div class="smallBox">
        <el-tag>{{ questionTable[titleIndex].title }} [ {{queIndex+1}} ] </el-tag
        >题目：
        <!-- {{ questionTable[titleIndex].subject[queIndex].question }} -->
         <prism-editor
            class="my-editor height-300"
            v-model="questionTable[titleIndex].subject[queIndex].question"
            :highlight="highlighter"
            :line-numbers="false"
            readonly
          ></prism-editor>
      </div>
      <div class="smallBox">
        <!-- <el-input
          v-model="textarea1"
          :autosize="{ minRows: 2 }"
          type="textarea"
          placeholder="Please input"
        /> -->
        作答：
        <prism-editor
            class="my-editor height-300"
            v-model="textarea1"
            :highlight="highlighter"
            :line-numbers="true"
           
          ></prism-editor>
      </div> 
      <div class="smallBox">
        答案：
        <el-switch
          v-model="isShow"
          inline-prompt
          size="large"
          active-text="显示"
          inactive-text="隐藏"
        />
        <div v-if="isShow">
            <prism-editor
            class="my-editor height-300"
            v-model="questionTable[titleIndex].subject[queIndex].answer"
            :highlight="highlighter"
            :line-numbers="false"
            readonly
          ></prism-editor>
          <!-- {{ questionTable[titleIndex].subject[queIndex].answer }} -->
        </div>

        <!-- <el-input
          v-model="textarea2"
          :autosize="{ minRows: 2 }"
          type="textarea"
          placeholder="Please input"
        /> -->
      </div>
    </div>
    <div class="footer">
      <el-button type="primary" @click="handleStep(0)" :disabled="questionTable.length===0">上一题</el-button>
      <div>跳转到第 <el-input
      v-model="queInput"
      type="number"
      @change="handleQueIndex"
      class="w-50 m-2"
      size="small"
    
    />题</div>
      <el-button type="primary" @click="handleStep(1)" :disabled="questionTable.length===0">下一题</el-button>
    </div>
  </div>
</template>

<script lang="ts">
import { Options, Vue } from "vue-class-component";
// import HelloWorld from '@/components/HelloWorld.vue'; // @ is an alias to /src
import { ref, watch } from "vue";
import { toRaw } from "@vue/reactivity";
import { PrismEditor } from "vue-prism-editor";
import "vue-prism-editor/dist/prismeditor.min.css";
import { highlight, languages } from "prismjs/components/prism-core";
import "prismjs/components/prism-clike";
import "prismjs/components/prism-javascript";
import "prismjs/themes/prism-tomorrow.css";
import { el } from "element-plus/es/locale";
// const checkboxGroup1 = ref(['Shanghai'])
// const cities = ['Shanghai', 'Beijing', 'Guangzhou', 'Shenzhen']
@Options({
  components: {
    // HelloWorld,
        PrismEditor,

  },
})
export default class Home extends Vue {
  checkTitle = [];
  isRandom = true;
  isShow = false;
  textarea1 = ref("");
  textarea2 = ref("");
  formInfo1 = [];
  questionTable = [];
  titleIndex = 0;
  queIndex = 0;
  queInput=0;
  mounted() {
    this.formInfo1 = JSON.parse(sessionStorage.getItem("formInfo1"))? JSON.parse(sessionStorage.getItem("formInfo1")):this.formInfo1;
    if(this.formInfo1.length>0){
      this.checkTitle.push(this.formInfo1[0].title);
        this.questionTable.push(this.formInfo1[0]);
    }
   
    watch(
      () => this.checkTitle,
      (curVal, preVal) => {
        this.questionTable = [];
        console.log('curVal',curVal.length);
        
        curVal.forEach((ele) => {
          var temp = this.formInfo1.find((item) => item.title === ele);
          if (temp && temp.subject.length > 0) {
            this.questionTable.push(temp);
          }
          // sessionStorage.setItem('formInfo1',JSON.stringify(toRaw(curVal)))
        });
      },
      {
        immediate: true,
        deep: true, // 深度监听的参数
        //配置项
      }
    );
     watch(
      () => this.queIndex,
      (curVal, preVal) => {
        this.queInput = curVal+1;
        
      },
      {
        immediate: true,
        deep: true, // 深度监听的参数
        //配置项
      }
    );
  }
  randomNum(min,max){
    var range = max - min;
    var rand = Math.random();
    var num = min + Math.round(rand * range);
    return num;
}
  handleStep(flag) {
    let i = this.queIndex;
    let j = this.titleIndex;
    if (this.isRandom) {
      this.titleIndex = this.randomNum(0,this.questionTable.length-1)
      this.queIndex=this.randomNum(0,this.questionTable[this.titleIndex].subject.length-1)
    } else {
      if (flag === 1) {
        this.queIndex = i === this.questionTable[j].subject.length - 1 ? 0 : i + 1;
        this.titleIndex =
          i === this.questionTable[j].subject.length - 1
            ? j === this.questionTable.length - 1
              ? 0
              : j + 1
            : j;
      } else {
        this.queIndex =
          i === 0
            ? j === 0
              ? this.questionTable[this.questionTable.length - 1].subject.length - 1
              : this.questionTable[j - 1].subject.length - 1
            : i - 1;
        this.titleIndex = i === 0 ? (j === 0 ? this.questionTable.length - 1 : j - 1) : j;
      }
    }
  }
    highlighter(code) {
    return highlight(code, languages.js); //returns html
  }
  handleQueIndex(){
    if(this.questionTable.length===0||this.queInput>this.questionTable[this.titleIndex].subject.length||this.queInput<1){
      this.queIndex=0
      this.queInput=1
    }else{
      this.queIndex=this.queInput-1
      // this.queInput=0
    }
    

  }

  // increase() {
  // }
  // get doubleCount(): number {
  //   return this.count * 2;
  // }
}
</script>
<style scoped lang="scss">
.head {
  display: flex;
  height: 50px;
  align-items: center;
  justify-content: center;
  .el-checkbox-group {
    margin-right: 20px;
  }
}
.main {
  margin: 20px 0;
  text-align: left;
}
.smallBox {
  margin-bottom: 20px;
  .el-tag {
    margin-right: 10px;
  }
}
.footer {
  display: flex;
  justify-content: space-around;
  ::v-deep .el-input__wrapper{
    width: 40px;
    margin: 0 5px;
  }
  ::v-deep .el-input__inner{
  text-align: center;
  }
  .el-input--small{
    display: inline;
  }
}
.my-editor {
  background: #efefef ;
  color: #2d2d2d;
  font-family: Fira code, Fira Mono, Consolas, Menlo, Courier, monospace;
  font-size: 14px;
  line-height: 1.5;
  padding: 5px;
  margin-top: 10px;
}
/* optional */
.prism-editor__textarea:focus {
  outline: none;
}
/* not required: */
.height-300 {
  height: auto;
}
</style>
